package com.sgb.kjwl.view.ui.activity.self.logistics;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityBiddingOrderBinding;
import com.sgb.kjwl.model.entity.self.BiddingOrderEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.transport.BiddingOrderDetailsActivity;
import com.sgb.kjwl.view.ui.adapter.self.logistics.BiddingOrderAdapter;
import com.sgb.kjwl.viewmodel.self.logistics.BiddingOrderModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Time:2021/7/15
 * Author:黄淋
 * Description:竞价订单列表
 */
public class BiddingOrderActivity extends AppActivity {

    ActivityBiddingOrderBinding mBinding;
    private BiddingOrderAdapter mAdapter;
    private BiddingOrderModel mModel;

    private boolean isLoadMore = false;
    private Map<String, Object> mMap;
    private boolean mIsGoodsOwner;//是否是货主 否是承运方 二者获取列表的接口不同

    public static void start(Context context, Boolean isGoodsOwner) {
        Intent intent = new Intent(context, BiddingOrderActivity.class);
        intent.putExtra("isGoodsOwner", isGoodsOwner);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_bidding_order);
        mModel = new ViewModelProvider(this).get(BiddingOrderModel.class);
    }

    @Override
    protected void init() {
        mMap = new HashMap<>();
        mIsGoodsOwner = getIntent().getBooleanExtra("isGoodsOwner", true);
        initAdapter();
        int type = UserOperating.getInstance().getUserIdentity();
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        String userNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        if (type == 1) {//企业
            mMap.put("terminal", "1");
            mMap.put("companyNo", companyNo);
            mMap.put("teamNo", "");
            mMap.put("userNo", "");
        } else if (type == 2) {//团队
            mMap.put("terminal", "2");
            mMap.put("teamNo", companyNo);
            mMap.put("companyNo", "");
            mMap.put("userNo", "");
        } else {//个人
            mMap.put("terminal", "3");
            mMap.put("userNo", userNo);
            mMap.put("companyNo", "");
            mMap.put("teamNo", "");
        }
        if (mIsGoodsOwner) {
            mModel.getBiddingOrderOwnerList(isLoadMore, mMap);
        } else {
            mModel.getBiddingOrderCarriageList(isLoadMore, mMap);
        }
        mModel.showLoading.observe(this, integer -> {
            switch (integer) {
                case 1:
                    DialogHelper.showProgressDialog(BiddingOrderActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;

            }
        });

        mModel.ldOrderList.observe(this, this::showBiddingOrderList);

    }

    private void initAdapter() {
        mAdapter = new BiddingOrderAdapter(this, null, mIsGoodsOwner);
        mBinding.displayViewBiddingOrder.setAdapter(mAdapter);
        mBinding.displayViewBiddingOrder.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setOnItemClick(new AdapterOnItemClick<BiddingOrderEntity>() {
            @Override
            public void onItemClick(BiddingOrderEntity biddingOrderEntity, int position) {
                BiddingOrderDetailsActivity.start(BiddingOrderActivity.this, biddingOrderEntity.getOrderNo(), mIsGoodsOwner ? 0 : 1);
            }
        });

        //加载更多、刷新
        mBinding.displayViewBiddingOrder.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                if (mIsGoodsOwner) {
                    mModel.getBiddingOrderOwnerList(isLoadMore, mMap);
                } else {
                    mModel.getBiddingOrderCarriageList(isLoadMore, mMap);
                }
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                if (mIsGoodsOwner) {
                    mModel.getBiddingOrderOwnerList(isLoadMore, mMap);
                } else {
                    mModel.getBiddingOrderCarriageList(isLoadMore, mMap);
                }
            }
        });

    }


    /**
     * 竞价订单数据
     *
     * @param list
     */
    public void showBiddingOrderList(List<BiddingOrderEntity> list) {
        mBinding.displayViewBiddingOrder.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                mBinding.displayViewBiddingOrder.loadMoreComplete();
                mAdapter.addDatas(list);
            } else {
                mBinding.displayViewBiddingOrder.setNoMore(true);
            }
            mBinding.displayViewBiddingOrder.setVisibility(View.VISIBLE);
        } else {
            mBinding.displayViewBiddingOrder.refreshComplete();
            if (null != list && list.size() > 0) {
                mBinding.displayViewBiddingOrder.setVisibility(View.VISIBLE);
                mBinding.errorView.setVisibility(View.GONE);
                mAdapter.setDatas(list);
                mBinding.displayViewBiddingOrder.setLoadingMoreEnabled(true);
            } else {
                mAdapter.setDatas(null);
                mBinding.displayViewBiddingOrder.setVisibility(View.GONE);
                mBinding.errorView.setVisibility(View.VISIBLE);
            }
        }
    }

}
